import echarts from 'echarts';

export interface EChartOption {
    title?: EChartTitleOption;
    legend?: Object;
    grid?: Object;
    xAxis?: Object;
    yAxis?: Object;
    polar?: Object;
    radiusAxis?: Object;
    angleAxis?: Object;
    radar?: Object;
    dataZoom?: Array<Object>;
    visualMap?: Array<Object>;
    tooltip?: Object;
    toolbox?: Object;
    geo?: Object;
    parallel?: Object;
    parallelAxis?: Object;
    timeline?: Object;
    series?: Array<Object>;
    color?: Array<Object>;
    backgroundColor?: string;
    textStyle?: Object;
    animation?: boolean;
    animationDuration?: number;
    animationEasing?: string;
    animationDurationUpdate?: number;
    animationEasingUpdate?: string;
}

export interface EChartTitleOption {
    show?: boolean;
    text?: string;
    link?: string;
    target?: string;
    textStyle?: Object;
    subtext?: string;
    sublink?: string;
    subtarget?: string;
    subtextStyle?: Object;
    padding?: number;
    itemGap?: number;
    zlevel?: number;
    z?: number;
    left?: string;
    top?: string;
    right?: string;
    bottom?: string;
    backgroundColor?: string;
    borderColor?: string;
    borderWidth?: number;
    shadowBlur?: number;
    shadowColor?: number;
    shadowOffsetX?: number;
    shadowOffsetY?: number;
}
export interface LinearGradient {
    colorStops: Array<Object>;
    global: boolean;
    type: string;
    x: number;
    x2: number;
    y: number;
    y2: number;
}

export interface SelectionType {
    label: string;
    value: string;
}
// 产生随机数
export const random = (amount: number = 0, range: number[]): number[] => {
    if (!Array.isArray(range) || range.length !== 2 || range.some(n => !Number.isInteger(n))) {
        throw new Error('need correct range');
    }
    if (!Number.isInteger(amount) || amount < 0) {
        throw new Error('need correct amount');
    }

    let low: number = range[0];
    let high: number = range[1];
    if (high <= low) {
        throw new Error('need correct range');
    }

    const result: number[] = [];
    for (let i = 0; i < amount; i++) {
        result.push(Math.floor(Math.random() * (high - low) + low));
    }
    return result;
};
// 通用配置
export const getCommonOption = (title: string): object => ({
    textStyle: {
        color: '#c8c8c8'
    },
    color: colors,
});
// 颜色库
export const colors: string[] =
    ['#77cad6', '#f8c880', '#ce97b0', '#97b16e', '#77cad6', '#f8c880', '#ce97b0', '#97b16e'];

export const mounthSelections: SelectionType[][] = [
    [
        { label: '全年', value: '0' },
        { label: '一月', value: '1' },
        { label: '二月', value: '2' },
        { label: '三月', value: '3' },
        { label: '四月', value: '4' },
        { label: '五月', value: '5' },
        { label: '六月', value: '6' },
        { label: '七月', value: '7' },
        { label: '八月', value: '8' },
        { label: '九月', value: '9' },
        { label: '十月', value: '10' },
        { label: '十一月', value: '11' },
        { label: '十二月', value: '12' }
    ]
];

export const dateIntervalSelections: SelectionType[][] = [
    [
        { label: '按周', value: '1' },
        { label: '按月', value: '2' },
        { label: '按季度', value: '3' },
        { label: '按年度', value: '4' }
    ]
];

export const subStationSelections: SelectionType[][] = [
    [
        { label: '经济开发区分局', value: 'jjkfqfj' },
        { label: '地铁分局', value: 'dtfj' },
        { label: '浦口区分局', value: 'pkfj' },
        { label: '玄武区分局', value: 'xwfj' },
        { label: '江宁区分局', value: 'jnqfj' },
    ]
]
    ;

export const hotSelections: SelectionType[][] = [
    [
        { label: '一级归口', value: 'level1' },
        { label: '二级归口', value: 'level2' },
        { label: '三级归口', value: 'level3' },
        { label: '四级归口', value: 'level4' }
    ],
    [
        { label: ' 全市', value: 'nanjing' },
        { label: '六合区', value: 'luhe' },
        { label: '栖霞区', value: 'qixia' },
        { label: '浦口区', value: 'pukou' },
        { label: '鼓楼区', value: 'gulou' },
        { label: '秦淮区', value: 'qinhua' },
        { label: '玄武区', value: 'xuanwu' },
        { label: '建邺区', value: 'jianye' },
        { label: '雨花台区', value: 'yuhua' },
        { label: '江宁区', value: 'jiangning' },
        { label: '溧水区', value: 'lishui' },
        { label: '高淳区', value: 'gaochun' },
    ],
    [
        { label: '全年', value: '0' },
        { label: '一月', value: '1' },
        { label: '二月', value: '2' },
        { label: '三月', value: '3' },
        { label: '四月', value: '4' },
        { label: '五月', value: '5' },
        { label: '六月', value: '6' },
        { label: '七月', value: '7' },
        { label: '八月', value: '8' },
        { label: '九月', value: '9' },
        { label: '十月', value: '10' },
        { label: '十一月', value: '11' },
        { label: '十二月', value: '12' }
    ]
];

export const govDepartmentSelection = [
    [
        { label: '政府机关', value: 'zfjg' },
        { label: '企事业单位', value: 'qsydw' },
        { label: '区政府（园区）', value: 'qzfyq' }
    ],
    [
        { label: ' 全市', value: 'nanjing' },
        { label: '六合区', value: 'luhe' },
        { label: '栖霞区', value: 'qixia' },
        { label: '浦口区', value: 'pukou' },
        { label: '鼓楼区', value: 'gulou' },
        { label: '秦淮区', value: 'qinhua' },
        { label: '玄武区', value: 'xuanwu' },
        { label: '建邺区', value: 'jianye' },
        { label: '雨花台区', value: 'yuhua' },
        { label: '江宁区', value: 'jiangning' },
        { label: '溧水区', value: 'lishui' },
        { label: '高淳区', value: 'gaochun' },
    ]

];

export const orgKindSelections = [
    [
        { label: '公共服务', value: 'ggfw' },
        { label: '企事业单位', value: 'qsydw' },

    ]
];

export const dateSelections = [
    [
        {
            label: 'date',
            value: 'dateInit'
        }

    ]
];
// 南京下属行政区
export const districts: string[] = ['六合区', '栖霞区', '浦口区', '鼓楼区', '秦淮区', '玄武区', '建邺区', '雨花台区', '江宁区', '溧水区', '高淳区'];

// 初始化图表
export const initGraph = (title: string, container: string, options: Object) => {
    let initTarget = document.getElementById(container) as HTMLDivElement | HTMLCanvasElement;
    const graph = echarts.init(initTarget, undefined, { renderer: 'svg' });
    const commonOption = getCommonOption(title);
    const totalOptions = { ...commonOption, ...options } as EChartOption;
    graph.setOption(totalOptions);
    return graph;
};

export function minMaxNormalize(
    value: number,
    min: number,
    max: number,
    lowerBound: number,
    upperBound: number
) {
    return (value - min) / (max - min) * (upperBound - lowerBound) + lowerBound;
}
export function batchMinMaxNormalize(
    arr: Array<number>,
    lowerBound: number,
    upperBound: number
): Array<number> {
    // 计算数组的上下界
    let min = Math.min(...arr);

    let max = Math.max(...arr);

    return arr.map(num => {
        return minMaxNormalize(num, min, max, lowerBound, upperBound);
    });
}
// 产生随机数
export const randNum = (amount: number = 0, range: number[]): number[] => {
    if (!Array.isArray(range) || range.length !== 2 || range.some(n => !Number.isInteger(n))) {
        throw new Error('need correct range');
    }
    if (!Number.isInteger(amount) || amount < 0) {
        throw new Error('need correct amount');
    }

    let low: number = range[0];
    let high: number = range[1];
    if (high <= low) {
        throw new Error('need correct range');
    }

    const result: number[] = [];
    for (let i = 0; i < amount; i++) {
        result.push(Math.floor(Math.random() * (high - low) + low));
    }
    return result;
};

export function getEchartsLinearColor(firstColor: string, secondColor: string): LinearGradient {
    return new echarts.graphic.LinearGradient(0, 0, 1, 1, [
        {
            offset: 0,
            color: firstColor
        },
        {
            offset: 1,
            color: secondColor
        }
    ]);
}

const evenColor = getEchartsLinearColor('#9198F0', '#ABD2EC');
const oddColor = getEchartsLinearColor('#FBE28F', '#FAA86A');

const otherColor = getEchartsLinearColor('#93BC66', '#CCEB81');

// 柱状图颜色渐变
const barEvenColor = getEchartsLinearColor('#A454BD', '#8AD0EE');
const barOddColor = getEchartsLinearColor('#EA4C96', '#DEA8E3');

export const colorChunk: LinearGradient[] = [evenColor, oddColor, barEvenColor, barOddColor, otherColor];

export const setStore = (name: string, content: {} | string) => {
    let payload: string = '';
    if (!name) {
        return;
    }
    if (typeof content !== 'string') {
        payload = JSON.stringify(content);
    }
    window.localStorage.setItem(name, payload);
};

export const getStore = (name: string) => {
    if (!name) {
        return;
    }
    return window.localStorage.getItem(name);
};

export const clearStore = () => {
    return window.localStorage.clear();
};

export const removeStore = (name: string) => {
    if (!name) {
        return;
    }
    window.localStorage.removeItem(name);
};

export function objectToArray(obj: {}) {
    let arr = [];
    for (let i in obj) {
        if (!!i) {
            arr.push(obj[i]);
        }
    }
    return arr;
}
// 数字数组排序
export function numberSort(arr: number[]): number[] {
    arr.sort((a, b) => {
        return a - b;
    });
    return arr;
}
// 数字数组比较
export function numberArrCompare(arr1: number[], arr2: number[]): boolean {
    return (arr1.length === arr2.length) && arr1.every((v: number, i: number) => {
        return v === arr2[i];
    });
}

export function genStateStr(arr: number[], states: string[]) {
    let str: string = '';
    arr.forEach((v: number, i: number) => {
        if (i === arr.length - 1) {
            str += states[v - 1];
        } else {
            str += states[v - 1] + '、';
        }
    });
    // str.substring(0, str.length - 2);
    return str;
}
// 重构列表数组

export const HandleStatisticsTitles = [
    '工单编号'
    , '诉求号码'
    , '姓名'
    , '性别'
    , '联系号码1'
    , '联系号码2'
    , '来电时间'
    , '诉求来源'
    , '诉求事件发生时间'
    , '是否紧急重大事件'
    , '信息是否保密'
    , '诉求地址'
    , '诉求内容'
    , '诉求目的'
    , '关键字'
    , '诉求类型'
    , '归口类型'
    , '回访类型'];

export const CaseFlowTitles = [
    '步骤',
    '办理部门',
    '办理人员',
    '处理方式',
    '办理意见',
    '接受部门',
    '接收人员',
    '收到时间',
    '处理时间',
    '耗费时间',
];

export const CaseHandleTitles = [
    '派发至',
    '签收时间',
    '反馈时间',
    '是否解决',
    '是否立案/立项',
    '何人',
    '何时',
    '何方式',
    '其他方式',
    '办理情况',
    '答复脚本',
    '证明文件'
];

export const OverTimeTitles = [
    '内容',
    '编号',
    '工单状态',
    '超期状态',
    '最近处理时间',
];

export const OverTimeStates = '签收、交办、退单、办结';